package com.cskaoyan.service.wxfree;

import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wxfree.WxComment;
import com.cskaoyan.bean.wxfree.WxCommentExample;
import com.cskaoyan.mapper.WxCommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.utils.Converter;
import com.cskaoyan.utils.PojoUtils;
import com.cskaoyan.vo.wxfree.WxCommentCount;
import com.cskaoyan.vo.wxfree.WxCommentListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-12 09:24
 **/
// 微信端评论模块
@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    WxCommentMapper wxCommentMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public WxCommentListVo commentList(Integer valueId, Byte type, Integer size, Integer page, Boolean showType) {
        // type=0为商品评论；type=1是专题评论；type=3是订单商品评论；

        // 分页
        PageHelper.startPage(page,size);
        // 查询条件
        WxCommentExample wxCommentExample = new WxCommentExample();
        WxCommentExample.Criteria criteria = wxCommentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId).andTypeEqualTo(type).andHasPictureEqualTo(showType).andDeletedEqualTo(false);
        // 查询
        List<WxComment> wxComments = wxCommentMapper.selectByExample(wxCommentExample);
        ArrayList<WxCommentListVo.DataBean> dataBeans = new ArrayList<>();

        for (WxComment wxComment : wxComments) {
            WxCommentListVo.DataBean dataBean = new WxCommentListVo.DataBean();
            // 查询用户信息
            User user = userMapper.selectByPrimaryKey(wxComment.getUserId());
            // 类型转换
            WxCommentListVo.DataBean.UserInfoBean userInfoBean = new WxCommentListVo.DataBean.UserInfoBean();
            Converter converter = PojoUtils.converter();
            converter.converter(user,userInfoBean);
            // 封装参数
            dataBean.setUserInfo(new WxCommentListVo.DataBean.UserInfoBean(user.getNickname(),user.getAvatar()));
            dataBean.setAddTime(wxComment.getAddTime());
            dataBean.setContent(wxComment.getContent());
            dataBean.setPicList(wxComment.getPicUrls());

            dataBeans.add(dataBean);
        }

        PageInfo<WxComment> wxCommentPageInfo = new PageInfo<>(wxComments);
        long count = wxCommentPageInfo.getTotal();

        return new WxCommentListVo(count,page,dataBeans);
    }

    @Override
    public WxCommentCount commentCount(Integer valueId, Byte type) {
        WxCommentExample wxCommentExample = new WxCommentExample();
        WxCommentExample.Criteria criteria = wxCommentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
        // 查询全部评论的个数
        List<WxComment> wxComments = wxCommentMapper.selectByExample(wxCommentExample);
        // 查询带图片评论的个数
        int hasPicCount = 0;
        for (WxComment wxComment : wxComments) {
            if(wxComment.getHasPicture()){
                hasPicCount++;
            }
        }

        return new WxCommentCount(hasPicCount, wxComments.size());
    }
}
